package com.wlz.creational.singleton.hunger;

import java.io.Serializable;

/**
 * @author： wlz
 * @Date 2020/7/21 10:02
 * @description： TODO
 * @version: 1.0
 *  饿汉式 （饿汉式(静态代码块)
 *  跟静态常量方式没有太大区别
 */
public class HungrySingleton02 {


    // step1:构造器私有化,防止用new创建实例
    private HungrySingleton02(){}

    // step2:声明实例变量
    private final static HungrySingleton02 singleton;

    // //step3:在静态代码块中初始化对象
    static {
        singleton = new HungrySingleton02();
    }

    // step4:提供一个public的静态方法，返回实例对象
    public static HungrySingleton02 getInstance() {
        return singleton;
    }

    public static void main(String[] args) {
        HungrySingleton02 oject1= HungrySingleton02.getInstance();
        HungrySingleton02 oject2= HungrySingleton02.getInstance();
        System.out.println(oject1.hashCode());
        System.out.println(oject2.hashCode());
    }

    //防止序列化工具
//    public Object readResolve(){
//        return (Object) instance;
//    }
}
